'''
Created on 2009-6-12

@author: roamer
'''

import Image
import os
from Setting import *
from ValueStore import *

class Statistics:
    def __init__(self):
        self.__result = []
    
    def __processOneImg(self,file):
        black_count = 0;
        x_sum = 0;
        y_sum = 0;
        img = Image.open(file)
        x,y = img.size
        for i in xrange(0,x):
            for j in xrange(0,y):
                pixel = img.getpixel((i,j))
                if(isinstance(pixel,tuple)):
                    pixel = 255 - pixel[len(pixel) - 1]
                if pixel < 100:
                    black_count += 1
                    x_sum += i
                    y_sum += j
        #avoid empty image
        if black_count == 0:
            black_count = 1
        name = file[file.rindex("\\")+1:file.rindex(".")]
        self.__result.append((black_count,float(x_sum) / float(black_count),float(y_sum) / float(black_count),name))
    
    def StatisticsFiles(self):
        import glob,os
        folder = Setting["ImgFolder"]
        files = glob.glob(folder + os.sep + "*" + Setting['ImgExt'])
        for i in files:
            self.__processOneImg(i)
            
    def SaveResult(self):
        data = self.__result
        step = Setting['Step']
        index = 1
        ret = {}
        while (index - 1) * step < 1024:
            minx = 32
            miny = 32
            maxx = 0
            maxy = 0
            start = (index - 1) * step
            end = index * step
            for item in data:
                if(item[0] > start and item[0] <= end):
                    if minx > item[1]:
                        minx = item[1]
                    if miny > item[2]:
                        miny = item[2]
                    if maxx < item[1]:
                        maxx = item[1]
                    if maxy < item[2]:
                        maxy = item[2]
            ret[str(end)] = ((minx,miny),(maxx,maxy))
            index += 1
        ValueStore.Save(Setting["Img"],{Setting["ImgKey"] : self.__result,Setting["ImgKey_Max_Min"] : ret})
    
    def RestoreResult(self):
        return ValueStore.Read(Setting["Img"])

class BaseProcess:
    def __init__(self):
        self.__result = {}
    def Process(self):
        import glob,os
        folder = Setting["BaseFolder"]
        files = glob.glob(folder + os.sep + "*" + Setting['ImgExt'])
        for i in files:
            self.__processOneImg(i)
    def __processOneImg(self,file):
        minx = 32
        maxx = 0
        miny = 32
        maxy = 0
        img = Image.open(file)
        x,y = img.size
        for i in xrange(0,x):
            for j in xrange(0,y):
                pixel = img.getpixel((i,j))
                if(isinstance(pixel,tuple)):
                    pixel = 255 - pixel[len(pixel) - 1]
                if pixel < 150:
                    if(minx > i):
                        minx = i
                    if(maxx < i):
                        maxx = i
                    if(miny > j):
                        miny = j
                    if(maxy < j):
                        maxy = j
        name = file[file.rindex("\\")+1:file.rindex(".")]
        self.__result[name] = (minx,miny,maxx,maxy)
    def SaveResult(self):
        ValueStore.Save(Setting["BaseKey"], self.__result)
    
    def RestoreResult(self):
        return ValueStore.Read(Setting["BaseKey"])